package org.jeecg.modules.rental.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.rental.entity.RentalAccountCoupon;
import org.jeecg.modules.rental.entity.RentalAccountPackage;
import org.jeecg.modules.rental.service.IRentalAccountCouponService;
import org.jeecg.modules.rental.vo.AccountCouponVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 账户卡券
 * @Author: sean
 * @Date:   2024-08-19
 * @Version: V1.0
 */
@Api(tags="账户优惠卷")
@RestController
@RequestMapping("/rental/account-coupon")
@Slf4j
public class RentalAccountCouponController extends JeecgController<RentalAccountCoupon, IRentalAccountCouponService> {
	@Autowired
	private IRentalAccountCouponService rentalAccountCouponService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rentalAccountCoupon
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	//@AutoLog(value = "账户优惠卷-分页列表查询")
	@ApiOperation(value="账户优惠卷-分页列表查询", notes="账户优惠卷-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="rental/account/couponlist")
	public Result<IPage<RentalAccountCoupon>> queryPageList(RentalAccountCoupon rentalAccountCoupon,
															@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
															@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
															HttpServletRequest req) {
		QueryWrapper<RentalAccountCoupon> queryWrapper = QueryGenerator.initQueryWrapper(rentalAccountCoupon, req.getParameterMap());
		Page<RentalAccountCoupon> page = new Page<RentalAccountCoupon>(pageNo, pageSize);
		IPage<RentalAccountCoupon> pageList = rentalAccountCouponService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rentalAccountCoupon
	 * @return
	 */
	@AutoLog(value = "账户优惠卷-添加")
	@ApiOperation(value="账户优惠卷-添加", notes="账户优惠卷-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RentalAccountCoupon rentalAccountCoupon) {
		rentalAccountCouponService.save(rentalAccountCoupon);
		return Result.OK("common.add.success");
	}
	
	/**
	 *  编辑
	 *
	 * @param rentalAccountCoupon
	 * @return
	 */
	@AutoLog(value = "账户优惠卷-编辑")
	@ApiOperation(value="账户优惠卷-编辑", notes="账户优惠卷-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RentalAccountCoupon rentalAccountCoupon) {
		rentalAccountCouponService.updateById(rentalAccountCoupon);
		return Result.OK("common.edit.success");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "账户优惠卷-通过id删除")
	@ApiOperation(value="账户优惠卷-通过id删除", notes="账户优惠卷-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rentalAccountCouponService.removeById(id);
		return Result.OK("common.delete.success");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "账户优惠卷-批量删除")
	@ApiOperation(value="账户优惠卷-批量删除", notes="账户优惠卷-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rentalAccountCouponService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("common.delete.batch.success");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "账户优惠卷-通过id查询")
	@ApiOperation(value="账户优惠卷-通过id查询", notes="账户优惠卷-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RentalAccountCoupon> queryById(@RequestParam(name="id",required=true) String id) {
		RentalAccountCoupon rentalAccountCoupon = rentalAccountCouponService.getById(id);
		if(rentalAccountCoupon==null) {
			return Result.error("common.query.not.found",null);
		}
		return Result.OK(rentalAccountCoupon);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rentalAccountCoupon
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RentalAccountCoupon rentalAccountCoupon) {
        return super.exportXls(request, rentalAccountCoupon, RentalAccountCoupon.class, "账户优惠卷");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RentalAccountCoupon.class);
    }

	/**
	 * 获取用户有效卡券
	 * @return
	 */
	@ApiOperation(value = "账户套餐-获取用户有效卡券", notes = "账户套餐-获取用户有效卡券")
	@GetMapping(value = "/effective-coupons")
	public Result<List<AccountCouponVO>> effectiveCoupons(@RequestParam(name = "price", required = true) BigDecimal price) {
		List<AccountCouponVO> list = rentalAccountCouponService.effectiveCoupons(price);
		return Result.OK(list);
	}

}
